package cn.wisco.web;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

public class Functions {
	public static String fmtNumberString(String str, String fmt) {
		if (str == null || fmt == null)
			return str;
		NumberFormat nf = new DecimalFormat(fmt);
		String result = str;
		try {
			result = nf.format(Double.valueOf(str));
		} catch (Exception e) {
		}
		return result;
	}

	public static String fmtNumber(Number str, String fmt) {
		if (str == null || fmt == null)
			return str == null ? "" : str.toString();
		NumberFormat nf = new DecimalFormat(fmt);
		String result = "";
		try {
			result = nf.format(str);
		} catch (Exception e) {
		}
		return result;
	}

	public static String fmtDate(Date d, String fmt) {
		if (d == null)
			return "";
		SimpleDateFormat df = new SimpleDateFormat(fmt);
		return (df.format(d));
	}

	public static String fmtString(int length, String str) {
		if (str == null)
			return "";
		if (str.length() < length)
			return str;
		else
			return str.substring(0, length) + "...";
	}

	public static int sizeOf(Collection c) {
		if (c == null)
			return 0;
		return c.size();
	}

	public static int sizeOf(Map m) {
		if (m == null)
			return 0;
		return m.size();
	}
	
	public static int length(String m) {
		if (m == null)
			return 0;
		return m.length();
	}
}
